package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.huang.*;
import com.csun.dsms.service.HgFactoryDataService;
import com.csun.dsms.service.HgFactoryIndustrialService;
import com.csun.dsms.service.HgGdpDataService;
import com.csun.dsms.utils.PublicUtil;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.List;

/**
 * 归上工业
 */
@RestController
@RequestMapping("/hgFactory")
public class HgFactoryController {

    @Resource
    private HgFactoryDataService hgFactoryDataService;

    @Resource
    private HgFactoryIndustrialService hgFactoryIndustrialService;

    /*
    * 设备的添加
    * */
    @Authorization
    @RequestMapping(value = "/dataAdd",method = RequestMethod.POST)
    public Wrapper dataAdd (@RequestBody HgFactoryData hgFactoryData){
        if(PublicUtil.isEmpty(hgFactoryData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFactoryDataService.save(hgFactoryData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 数据查询
     * */
    @Authorization
    @RequestMapping(value = "/dataListQuery",method = RequestMethod.POST)
    public Wrapper dataListQuery (@RequestBody HgQueryDto hgQueryDto){
        List<HgFactoryData> query = hgFactoryDataService.queryDataList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导入工业的数据
     */
    @Authorization
    @RequestMapping(value = "/importFactoryData",method = RequestMethod.POST)
    public Wrapper importFactoryData (@RequestParam("file") MultipartFile file) {
        return hgFactoryDataService.importFactoryData(file);
    }

    @Authorization
    @RequestMapping(value = "/dataUpdate",method = RequestMethod.POST)
    public Wrapper dataUpdate (@RequestBody HgFactoryData hgFactoryData){
        if(PublicUtil.isEmpty(hgFactoryData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFactoryDataService.update(hgFactoryData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * */
    @Authorization
    @RequestMapping(value = "/industrialDataAdd",method = RequestMethod.POST)
    public Wrapper industrialDataAdd (@RequestBody HgFactoryIndustrialData hgFactoryIndustrialData){
        if(PublicUtil.isEmpty(hgFactoryIndustrialData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFactoryIndustrialService.save(hgFactoryIndustrialData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }

    /*
     * 数据查询
     * */
    @Authorization
    @RequestMapping(value = "/industrialListQuery",method = RequestMethod.POST)
    public Wrapper industrialListQuery (@RequestBody HgQueryDto hgQueryDto){
        List<HgFactoryIndustrialData> query = hgFactoryIndustrialService.queryDataList(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导入工业的主导产业数据
     */
    @Authorization
    @RequestMapping(value = "/importFactoryIndustrialData",method = RequestMethod.POST)
    public Wrapper importFactoryIndustrialData (@RequestParam("file") MultipartFile file) {
        return hgFactoryIndustrialService.importFactoryIndustrialData(file);
    }

    @Authorization
    @RequestMapping(value = "/industrialDataUpdate",method = RequestMethod.POST)
    public Wrapper dataUpdate (@RequestBody HgFactoryIndustrialData hgFactoryData){
        if(PublicUtil.isEmpty(hgFactoryData.getArea().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"县市区不能不空");
        }
        Integer save = hgFactoryIndustrialService.update(hgFactoryData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }



    @Authorization
    @RequestMapping(value = "/groupByMonth",method = RequestMethod.POST)
    public Wrapper groupByMonth (@RequestBody HgQueryDto hgQueryDto){
        List<HgYearMonthVo> query = hgFactoryDataService.groupByMonth(hgQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /**
     * 导出工业的数据模板
     */
    @GetMapping("/downFactoryTemplate")
    public void downFaiTemplate(HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();
        String worksheetTitle = "工业的数据导入模板表";
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时不换行
        cellStyleTitle.setWrapText(false);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时换行
        cellStyle.setWrapText(true);

        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeight((short) 200);
        cellStyleTitle.setFont(font);

        HSSFSheet sheet = wb.createSheet();
        // 定义第一行
        HSSFRow row1 = sheet.createRow(0);

        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计年份"));
        sheet.setColumnWidth(0, 20 * 256);;
        //第一行第2列
        cell1 = row1.createCell(1);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计月"));
        sheet.setColumnWidth(1, 20 * 256);
        //第一行第3列
        cell1 = row1.createCell(2);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("数据的区域"));
        sheet.setColumnWidth(2, 20 * 256);
        //第一行第4列
        cell1 = row1.createCell(3);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是市的还是区的"));
        sheet.setColumnWidth(3, 20 * 256);
        //第一行第5列
        cell1 = row1.createCell(4);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("总产值"));
        sheet.setColumnWidth(4, 20 * 256);
        //第一行第6列
        cell1 = row1.createCell(5);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增加值"));
        sheet.setColumnWidth(5, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(6);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增加值增速"));
        sheet.setColumnWidth(6, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(7);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上一级增速"));
        sheet.setColumnWidth(7, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(8);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增长率排名"));
        sheet.setColumnWidth(8, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(9);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("环比比的去年的排名"));
        sheet.setColumnWidth(9, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(10);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传的部门"));
        sheet.setColumnWidth(10, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(11);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传人"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(12);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是否审核"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(13);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("审核人"));
        sheet.setColumnWidth(11, 20 * 256);

        String fileName = worksheetTitle;
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出工业的主导产业数据模板
     */
    @GetMapping("/downFactoryIndustrialTemplate")
    public void downFactoryIndustrialTemplate(HttpServletResponse response) {
        HSSFWorkbook wb = new HSSFWorkbook();
        String worksheetTitle = "工业的主导产业数据导入模板表";
        // 创建单元格样式
        HSSFCellStyle cellStyleTitle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyleTitle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyleTitle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时不换行
        cellStyleTitle.setWrapText(false);
        // ------------------------------------------------------------------
        HSSFCellStyle cellStyle = wb.createCellStyle();
        // 指定单元格居中对齐
        cellStyle.setAlignment(HorizontalAlignment.CENTER);
        // 指定单元格垂直居中对齐
        cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
        // 指定当单元格内容显示不下时换行
        cellStyle.setWrapText(true);

        // ------------------------------------------------------------------
        // 设置单元格字体
        HSSFFont font = wb.createFont();
        font.setBold(true);
        font.setFontName("宋体");
        font.setFontHeight((short) 200);
        cellStyleTitle.setFont(font);

        HSSFSheet sheet = wb.createSheet();
        // 定义第一行
        HSSFRow row1 = sheet.createRow(0);

        HSSFCell cell1 = row1.createCell(0);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计年份"));
        sheet.setColumnWidth(0, 20 * 256);;
        //第一行第2列
        cell1 = row1.createCell(1);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("统计月"));
        sheet.setColumnWidth(1, 20 * 256);
        //第一行第3列
        cell1 = row1.createCell(2);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("数据的区域"));
        sheet.setColumnWidth(2, 20 * 256);
        //第一行第4列
        cell1 = row1.createCell(3);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是市的还是区的"));
        sheet.setColumnWidth(3, 20 * 256);
        //第一行第5列
        cell1 = row1.createCell(4);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("产业类型"));
        sheet.setColumnWidth(4, 20 * 256);
        //第一行第6列
        cell1 = row1.createCell(5);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增长点个数"));
        sheet.setColumnWidth(5, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(6);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("增速"));
        sheet.setColumnWidth(6, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(7);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上一级增速"));
        sheet.setColumnWidth(7, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(8);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("排名"));
        sheet.setColumnWidth(8, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(9);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("预增产值"));
        sheet.setColumnWidth(9, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(10);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("年产值"));
        sheet.setColumnWidth(10, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(11);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("拉动产业增长率"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(12);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传的部门"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(13);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("上传人"));
        sheet.setColumnWidth(14, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(15);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("是否审核"));
        sheet.setColumnWidth(11, 20 * 256);
        //第一行第7列
        cell1 = row1.createCell(16);
        cell1.setCellStyle(cellStyleTitle);
        cell1.setCellValue(new HSSFRichTextString("审核人"));
        sheet.setColumnWidth(11, 20 * 256);

        String fileName = worksheetTitle;
        try {
            fileName = new String(fileName.getBytes("utf-8"), "ISO8859-1");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.reset();
        response.setHeader("Content-Disposition", "attachment;fileName=" + fileName + ".xls");// 指定下载的文件名
        response.setContentType("application/vnd.ms-excel;charset=utf-8");
        try {
            OutputStream output = response.getOutputStream();
            wb.write(output);
            output.flush();
            output.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
